package Day13.Tests;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 斗地主发牌
 */
public class DouDiZhu {

    ArrayList<PuKe> ary = new ArrayList<>();

    /**
     * 初始化54张扑克牌
     */
    public void init(){
        String [] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String [] types = "♠,♥,♣,♦".split(",");
        for(int i = 0;i < points.length;i++){
            for (int j = 0;j < types.length;j++){
                ary.add(new PuKe(points[i],types[j],i+1,j+1));
            }
        }
        ary.add(new PuKe("大王","",14,0));
        ary.add(new PuKe("小王","",13,0));
    }




    /**
     * 洗牌
     */
    public void checkCard(){
        Collections.shuffle(ary);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<PuKe> player1 = new ArrayList<>(ary.subList(0,17));
        ArrayList<PuKe> player2 = new ArrayList<>(ary.subList(17,34));
        ArrayList<PuKe> player3 = new ArrayList<>(ary.subList(34,51));
        ArrayList<PuKe> base = new ArrayList<>(ary.subList(51,54));

        myComparator(player1);
        myComparator(player2);
        myComparator(player3);
        myComparator(base);

        System.out.println("玩家1的牌是" + player1);
        System.out.println("玩家2的牌是" + player2);
        System.out.println("玩家3的牌是" + player3);
        System.out.println("底牌是" + base);
        ArrayList<PuKe> diZhu = new ArrayList<>();
        diZhu.add(ary.get(51));
        diZhu.add(ary.get(52));
        diZhu.add(ary.get(53));
        ary = new ArrayList<>();
        for (PuKe obj : player1){
            ary.add(obj);
        }
        for (PuKe obj : player2){
            ary.add(obj);
        }
        for (PuKe obj : player3){
            ary.add(obj);
        }
        for (PuKe obj : diZhu){
            ary.add(obj);
        }
    }



    /**
     * 排序
     */
     public void myComparator(ArrayList ary){
         Collections.sort(ary, new Comparator<PuKe>() {
             @Override
             public int compare(PuKe o1, PuKe o2) {
                 if (o1.getNum() == o2.getNum()) {
                     return o1.getTypeValue() - o2.getTypeValue();
                 } else {
                    return o2.getNum() - o1.getNum();
                 }
             }
         });
     }


    /**
     * 开始
     */
    public void process(){
        init();
        checkCard();
        sendCard();
    }
}
